/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.inventory;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Class for holding item types
 * @author rothens
 */
public enum ItemType {
    ARMOR_HELMET(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_CHESTPLATE(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_GLOVES(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_BELT(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_LEGGINGS(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_BOOTS(Arrays.asList("armor"), Arrays.asList("enchants")),
    ARMOR_SHIELD(Arrays.asList("armor"), Arrays.asList("enchants")),
    JEWEL_NECKLACE(Arrays.asList("properties"), Arrays.asList("enchants")),
    JEWEL_BRACELET(Arrays.asList("properties"), Arrays.asList("enchants")),
    JEWEL_RING(Arrays.asList("properties"), Arrays.asList("enchants")),
    JEWEL_TRINKET(Arrays.asList("properties"), Arrays.asList("enchants")),
    WEAPON_MELEE(Arrays.asList("mindmg", "maxdmg"), Arrays.asList("enchants")),
    WEAPON_RANGED(Arrays.asList("mindmg", "maxdmg"), Arrays.asList("enchants")),
    OTHER_CONSUMABLE(Arrays.asList("effect")),
    OTHER_KEYS(),
    OTHER_QUEST(Arrays.asList("quests"), Arrays.asList("enchants")),
    OTHER_JUNK(Arrays.asList("sellprice"));
    
    LinkedList<String> mandatory;
    LinkedList<String> optional;
    
    ItemType(){
        mandatory = new LinkedList<String>();
        optional = new LinkedList<String>();
        addCommon();
    }
    
    ItemType(List<String> mandatory){
        this.mandatory = new LinkedList<String>(mandatory);
        optional = new LinkedList<String>();
        addCommon();
    }
    
    ItemType(List<String> mandatory, List<String> optional){
        this.mandatory = new LinkedList<String>(mandatory);
        this.optional = new LinkedList<String>(optional);
        addCommon();
    }
    
    private void addCommon(){
        mandatory.addAll(Arrays.asList("name", "id", "description", "type"));
        optional.addAll(Arrays.asList("price"));
    }
    
    
    /**
     * Get the needed fields for item construction.
     * @param fields The hashset contains the loaded parameters
     * @return The list of the parameters, or null, if a mandatory field is missing.
     */
    public LinkedList<String> getNeededFields(HashMap<String, Object> fields){
        LinkedList<String> ret = new LinkedList<String>();
        for(String s : mandatory){
            Object o = fields.get(s);
            if(o == null){
                System.err.println("Missing mandatory field!\nname: " + fields.get("name") + "\nid: " + fields.get("id") + "\nfield:" + s);
                return null;
            }
            if(o instanceof String){
                if(((String)o).isEmpty()){
                    System.err.println("Missing mandatory field!\nname: " + fields.get("name") + "\nid: " + fields.get("id") + "\nfield:" + s);
                    return null;
                }
            }
            ret.add(s);
        }
        for(String s : optional){
            Object o = fields.get(s);
            if(o == null){
                continue;
            }
            if(o instanceof String){
                if(((String)o).isEmpty()){
                    continue;
                }
            }
            ret.add(s);
        }
        return ret;
    }
    
}
